package org.ogv.usada.states;

import org.ogv.OGV;
import org.ogv.usada.models.Content;
import org.ogv.usada.models.GP;
import org.ogv.usada.models.LevelDef;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector3;

public class GameOverScreen extends ScreenState {
	private LevelDef 			_ld;
	private int					_lvIdx;
	private int 				_activeMedal;
	private TextureRegion 		_bg0, _bg1, _activeBG;
	private Sprite 				_retryBtn, _exitBtn, _nextBtn, _activeExitBtn, _thropy;
	private Sprite[]			_medals;
	private boolean				_isWin;
	private BitmapFont			_bmf;
	private int					_score;
	private Sound				_clap, _mainSound;
	

	public GameOverScreen(SpriteBatch sb) {
		super(sb);
		_id = GP.$().STATE_GAMEOVER;
	}

	@Override
	public void init() {
		//Initialize images
		_bmf		= OGV.asset.get(GP.$().RES_FONT_PRINTCLEARLY, BitmapFont.class);
		_bg0 		= OGV.asset.getTextureRegion(GP.$().RES_TEXR_GAMEOVER_BG_0);
		_bg1 		= OGV.asset.getTextureRegion(GP.$().RES_TEXR_GAMEOVER_BG_1);
		_retryBtn	= new Sprite(OGV.asset.getTextureRegion(GP.$().RES_TEXR_GAMEOVER_RETRY));
		_exitBtn	= new Sprite(OGV.asset.getTextureRegion(GP.$().RES_TEXR_GAMEOVER_EXIT));
		_nextBtn	= new Sprite(OGV.asset.getTextureRegion(GP.$().RES_TEXR_GAMEOVER_NEXT));
		_thropy		= new Sprite(OGV.asset.getTextureRegion(GP.$().RES_TEXR_GAMEOVER_THROPY));
		_medals		= new Sprite[GP.$().RES_TEXR_GAMEOVER_MEDALS.length];
		for(int i = 0; i < _medals.length; ++i) {
			_medals[i] = new Sprite(OGV.asset.getTextureRegion(GP.$().RES_TEXR_GAMEOVER_MEDALS[i]));
		}
		
		//fetch level def
		_lvIdx 	= (Integer) getStateMgr().popValueFromExchangeBuff();
		_ld		= Content.$().levels[_lvIdx];
		
		//fetch score
		_score = (Integer) getStateMgr().popValueFromExchangeBuff();
		_activeMedal = _ld.getScoreCategory(_score);
		_isWin = _activeMedal != 0;
		if(!_isWin) {
			//Init lose image placement
			_activeBG		= _bg1;
			_activeExitBtn 	= _exitBtn;
			_exitBtn.setPosition(1023, GP.$().GAME_HEIGHT - 466);
			_retryBtn.setPosition(156, GP.$().GAME_HEIGHT - 466);
			_medals[0].setPosition((GP.$().GAME_WIDTH - _medals[0].getWidth())/2, (GP.$().GAME_HEIGHT - _medals[0].getHeight())/2);
		} else {
			//Init win image placement
			_activeBG		= _bg0;
			_activeExitBtn 	= _nextBtn; 
			_retryBtn.setPosition(700, GP.$().GAME_HEIGHT - 759);
			_activeExitBtn.setPosition(1000, GP.$().GAME_HEIGHT - 759);
			_thropy.setPosition(815, GP.$().GAME_HEIGHT - 500);
			for(int i = 1; i < _medals.length; ++i) {
				_medals[i] = new Sprite(OGV.asset.getTextureRegion(GP.$().RES_TEXR_GAMEOVER_MEDALS[i]));
				_medals[i].setPosition(100, GP.$().GAME_HEIGHT - 700);
			}
		}
		
		initializeDoors();
		startOpeningDoor();
		playSounds();
	}
	
	private void playSounds() {
		if(_isWin) {
			_mainSound 	= OGV.asset.get("data/audio/win.mp3", Sound.class);
			if(_activeMedal == 4)
				_clap	= OGV.asset.get("data/audio/applause.mp3", Sound.class);
		} else {
			_mainSound 	= OGV.asset.get("data/audio/lose.mp3", Sound.class);
		}
		
		_mainSound.play(GP.$().SOUND_VOL);
		if(_clap != null) _clap.play(GP.$().SOUND_VOL);
	}
	
	@Override
	public void destroy() {
		_mainSound.stop();
		if(_clap != null) _clap.stop();
	}

	@Override
	public void run(float elapsedTime) {
		update(elapsedTime);
		_sb.begin();
		draw();
		drawDoors();
		_sb.end();
	}

	private void update(float elapsedTime) {
		if(areDoorsActive())
			updateDoors(elapsedTime);
		//update input
		else if(Gdx.input.justTouched()) {
			updateTouchPoint();
			if(retryPressed(_tempV3))  {
				GP.$().playClickSound();
				retryLevel();
			}
			else if(exitPressed(_tempV3)) {
				GP.$().playClickSound();
				exitLevel();
			}
		}
	}

	private boolean exitPressed(Vector3 touch) {
		return _activeExitBtn.getBoundingRectangle().contains(touch.x, touch.y);
	}

	private boolean retryPressed(Vector3 touch) {
		return _retryBtn.getBoundingRectangle().contains(touch.x, touch.y);
	}

	private void exitLevel() {
		//Go to level select
		requestStateChange(-2);
	}

	private void retryLevel() {
		//Pop this state, going to game
		getStateMgr().pushValueInExchangeBuff(new Integer(_lvIdx));
		requestStateChange(-1);
	}

	private void draw() {
		//Draw BG
		_sb.draw(_activeBG, 0, 0);
		
		//draw retry button
		_retryBtn.draw(_sb);
		
		//draw exit button
		_activeExitBtn.draw(_sb);
		
		//draw medal
		_medals[_activeMedal].draw(_sb);

		if(_isWin) {
			//draw trophy
			_thropy.draw(_sb);
			
			//draw score
			_bmf.draw(_sb, "" + _score, _thropy.getX() + (_thropy.getWidth()-_bmf.getBounds("" + _score).width)/2, GP.$().GAME_HEIGHT - 240 + _bmf.getLineHeight());
			
			//draw level completed
			_bmf.draw(_sb, "LEVEL COMPLETED", 700, GP.$().GAME_HEIGHT - 100 + _bmf.getLineHeight());
		} else {
			//draw you failed
			_bmf.draw(_sb, "YOU FAILED", 460, GP.$().GAME_HEIGHT - 131 + _bmf.getLineHeight());
		}
	}
}
